package psl;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 23, 2007
 * Time: 12:40:24 PM
 */
public class UserSession
{
    public ArrayList mvPages = new ArrayList ();
    public ArrayList mvSelsected = null;
    public HashMap mvQuantity= null;
    public String msSessionId= null;
/*
    public String[] maQuantity = null;
*/
    public String msTotal = null;
    public String msError = null;
    public String msConform = null;
    public String msCustId = null;
    public String msViewPage;
    public String msPageID;
    public String msLogin = null;
    boolean flag = false;

    public UserSession ()
    {
        mvSelsected = new ArrayList ();
        mvQuantity= new HashMap();
        flag = true;
    }

    public void processRequest (HashMap fhRequest)
    {
        System.out.println ("In ProcessRequest");
        String[] laAction = (String[]) fhRequest.get ("action_fld");
        int liAction = Integer.parseInt (laAction[0]);
        if (liAction == AppConstants.ACTN_OPEN)
        {
            boolean lbReset = true;
            if (laAction.length > 2)
            {
                if (laAction[2].equals ("N"))
                {
                    lbReset = false;
                }
            }
            performOpen (laAction[1], lbReset);
        }
        else if (liAction == AppConstants.ACTN_LOGIN)
        {
            performLogin (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_LOGOUT)
        {
            boolean lbReset = true;
            if (laAction.length > 1 && laAction[1].equals ("N"))
            {
                lbReset = false;
            }
            performLogOut (fhRequest, lbReset);
        }
        else if (liAction == AppConstants.ACTN_NEW_USER)
        {
            msError = null;
            performNewUser (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_CANCEL)
        {
            cancel (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_CONTINUE)
        {
            performContinue (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_BACK)
        {
            cancel (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_SET_VALUE)
        {
            setConstant(fhRequest);
        }
        else if (liAction == AppConstants.ACTN_DONE)
        {
            done ();
        }
        else if (liAction == AppConstants.ACTN_FINISH)
        {
            performFinish ();
        }
        else if (liAction == AppConstants.ACTN_PROCESS)
        {
            String[] laSelected = CommonServices.stringToArray (laAction[2], ",");
            performProcess (laSelected, "add");
        }
        else if (liAction == AppConstants.ACTN_UPDATE)
        {
            String[] laQuantity = CommonServices.stringToArray (laAction[1], ",");
            String[] laQuantityDis = CommonServices.stringToArray (laAction[2], ",");
            String[] laRemoveID = CommonServices.stringToArray (laAction[3], ",");
            String[] laProID = CommonServices.stringToArray (laAction[4], ",");
            if (laRemoveID[0].equals (""))
            {
                laRemoveID = null;
            }
            String[] laQty = new String[laQuantity.length];
            for (int i = 0; i < laQuantityDis.length; i++)
            {
                String[] qty = CommonServices.stringToArray (laQuantity[i], ":");
                String quntity = qty[0] + ":" + qty[1] + ":" + laQuantityDis[i]+":"+laProID[i];
                laQty[i] = quntity;
            }
            performUpdate (laRemoveID, laQty, laProID);
        }
        else if (liAction == AppConstants.ACTN_REMOVE)
        {
            String[] laRemove = CommonServices.stringToArray (laAction[2], ",");
            performProcess (laRemove, "Remove");
        }
        else if (liAction == AppConstants.ACTN_SAVE)
        {
            performSave (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_SEARCH)
        {
            boolean lbBookmark = "Y".equals (laAction[1]);
            performSearch (fhRequest, lbBookmark);
        }
/*
        else if (liAction == AppConstants.ACTN_SCROLL_TO_PAGE)
        {
            int liCurrentPage = Integer.parseInt (laAction[1]);
            performScrollToPage (fhRequest, liCurrentPage);
        }
        else if (liAction == AppConstants.ACTN_NEW_RECORD)
        {
            newRecord (fhRequest);
        }
        else if (liAction == AppConstants.ACTN_EDIT_RECORD)
        {
            int liSelectedRow = Integer.parseInt (laAction[1]);
            editRecord (fhRequest, liSelectedRow, false);
        }
*/
    }

    public void performOpen (String fsPageId, boolean fbReset)
    {
        BasePage loPage = null;
        if (fsPageId.equalsIgnoreCase ("ItemBill"))
        {
            loPage = new ItemBill ();
        }
        else if (fsPageId.equalsIgnoreCase ("loginPage"))
        {
            loPage = new LoginPage ();
        }
        else if (fsPageId.equalsIgnoreCase ("Registere"))
        {
            loPage = new RagistrationPage ();
        }
        else if (fsPageId.equalsIgnoreCase ("SaleEntry"))
        {
            loPage = new SaleEntry ();
        }
        else if (fsPageId.equalsIgnoreCase ("userAccount"))
        {
            loPage = new UserAccount ();
        }
        else
        {
            loPage = new ItemList ();
            msViewPage = loPage.getViewPage ();
            msPageID = fsPageId;
        }
        loPage.moUserSession = this;
        if (fbReset)
        {
            mvPages = new ArrayList ();
        }
        if (!mvPages.contains (loPage))
        {
            mvPages.add (loPage);
            System.err.println ("mvPages: " + mvPages);
        }
    }

    public void cancel (HashMap fhRequest)
    {
        if (mvPages.size () > 0)
        {
            msError = null;
            msConform = null;
            removeLastPage ();
        }
    }

    public void performProcess (String[] faSelected, String fsTag)
    {
        BasePage loPage = getCurrentPage ();
        loPage.miStartRow=(loPage.miPreRow!=null && loPage.miPreRow.size ()!=0)?
                (Integer)loPage.miPreRow.get (loPage.miPreRow.size ()-1):0;
        msError=null;
        loPage.performProcess(faSelected, fsTag);
    }

    public void done ()
    {
        BasePage loCurrentPage = getCurrentPage ();
        String lsDetailPageId = loCurrentPage.getDetailPageId ();
        if (mvSelsected != null && mvSelsected.size () != 0)
        {
            msError=null;
            loCurrentPage.setTempData (msCustId, mvSelsected);
            performOpen (lsDetailPageId, false);
        }
        else
        {
            loCurrentPage.miStartRow=(loCurrentPage.miPreRow!=null && loCurrentPage.miPreRow.size ()!=0)?
                    (Integer)loCurrentPage.miPreRow.get (loCurrentPage.miPreRow.size ()-1):0;
            msError="There is no Item in Cart";
        }
        BasePage loNewPage = getCurrentPage ();
        loNewPage.setParentPage (loCurrentPage);
    }

    public void performFinish ()
    {
/*
        String[] laQty = new String[laQuantity.length];
        for (int i = 0; i < laQuantity.length; i++)
        {
            String[] Qty = CommonServices.stringToArray (laQuantity[i], ":");
            laQty[i] = Qty[2];
        }
        maQuantity = laQty;
*/
        BasePage loCurrentPage = getCurrentPage ();
        String lsDetailPageId = loCurrentPage.getDetailPageId ();
        String lsExtraPageId = loCurrentPage.getExtraPageId ();
        if (msLogin != null && msLogin.equalsIgnoreCase ("login"))
        {
            performOpen (lsExtraPageId, false);
            loCurrentPage.modifyTempData (mvQuantity, mvSelsected);
        }
        else
        {
            performOpen (lsDetailPageId, false);
        }
        BasePage loNewPage = getCurrentPage ();
        loNewPage.setParentPage (loCurrentPage);
    }

    public void performNewUser (HashMap fhrequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        String lsExtraPageId = loCurrentPage.getExtraPageId ();
        performOpen (lsExtraPageId, false);
        BasePage loNewPage = getCurrentPage ();
        loNewPage.setParentPage (loCurrentPage);
    }

    public void removeLastPage ()
    {
        mvPages.remove (mvPages.size () - 1);
    }

    public BasePage getCurrentPage ()
    {
        if (mvPages.size () == 0)
        {
            return null;
        }
        BasePage loPage = (BasePage) mvPages.get (mvPages.size () - 1);
        return loPage;
    }

    private void performUpdate (String[] faRemove, String[] faQuantity, String[] faProId)
    {
        BasePage lopage = getCurrentPage ();
        if (faQuantity != null)
        {
            if (faRemove != null)
            {
                for (int i = 0; i < faRemove.length; i++)
                {
                    String[] remove = CommonServices.stringToArray (faRemove[i], ":");
                    mvSelsected.remove (remove[1]);
                    lopage.removeTempData (remove[1]);
                }
                if (mvSelsected.size () == 0)
                {
                    removeLastPage ();
                }
            }
            lopage.performUpdate (faRemove, faQuantity, faProId);
        }
    }

    public void performLogOut (HashMap fhRequest, boolean fbFlag)
    {
        if (fbFlag)
        {
            msLogin = null;
            msCustId = null;
            mvPages.removeAll (mvPages);
            mvSelsected = new ArrayList ();
            mvQuantity = null;
            msTotal = null;
            msError = null;
            msConform = null;
            msViewPage = null;
            msPageID = null;
            flag = false;
        }
        else
        {
            performOpen ("", true);
        }

    }

    public void setConstant(HashMap fhRequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        String[] lsTag = (String[]) fhRequest.get ("action_fld");
        loCurrentPage.setConstant (fhRequest/*lsTag[1]*/);
    }

    public void performLogin (HashMap fhRequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setRequest (fhRequest);
        boolean flag = loCurrentPage.performLogin ();
        String lsDetailPageId = loCurrentPage.getDetailPageId ();
        if (flag)
        {
            if (mvSelsected != null && mvSelsected.size () != 0)
            {
                msLogin = "login";
                performOpen (lsDetailPageId, false);
                BasePage loNewPage = getCurrentPage ();
                loNewPage.setParentPage (loCurrentPage);
                loNewPage.setRecored ();
            }
            else
            {
                msLogin = "login";
/*
                loCurrentPage.setRecored ();
*/
                performOpen ("", true);
            }
        }

    }

    public void performSave (HashMap fhRequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setRequest (fhRequest);
        loCurrentPage.performSave ();
    }

    public void performSearch (HashMap fhRequest, boolean fbBookmark)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setRequest (fhRequest);
        loCurrentPage.performSearch ();
    }

    public void performContinue (HashMap fhRequest)
    {
        String[] laAction = (String[]) fhRequest.get ("action_fld");
        String[] laValue = CommonServices.stringToArray (laAction[1], ":");
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.performContinue (laValue);
        performOpen ("", true);
    }
}
/*

    public void performScrollToPage (HashMap fhRequest, int fiCurrentPage)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.miCurrentPage = fiCurrentPage;
    }

    public void newRecord (HashMap fhRequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setRequest (fhRequest);
        String lsDetailPageId = loCurrentPage.getDetailPageId ();
        performOpen (lsDetailPageId, false);
        BasePage loNewPage = getCurrentPage ();
        loNewPage.setParentPage (loCurrentPage);
        loNewPage.performNewRecord ();
    }

    public void editRecord (HashMap fhRequest, int fiSelectedRow, boolean fbReadOnly)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setCurrentRow (fiSelectedRow);
        loCurrentPage.setRequest (fhRequest);
        String lsDetailPageId = loCurrentPage.getDetailPageId ();
        performOpen (lsDetailPageId, false);
        BasePage loNewPage = getCurrentPage ();
        loNewPage.setParentPage (loCurrentPage);
        loNewPage.performEditRecord ();
    }


    public void performSave (HashMap fhRequest)
    {
        BasePage loCurrentPage = getCurrentPage ();
        loCurrentPage.setRequest (fhRequest);
        loCurrentPage.performSave ();
        if (msError == null)
        {
            removeLastPage ();
        }
    }
*/
